package com.harisucici.app;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.module_base_core.baseview.BaseActivity;
import com.example.module_base_core.netbus.NetStatusBus;
import com.google.android.material.tabs.TabLayout;
import com.harisucici.app.databinding.ActivityMainBinding;
import com.harisucici.app.utils.FragmentUtils;

import java.util.ArrayList;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainModel> {
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private DemandAdapter mDemandAdapter;
    private ViewPager mMViewPager;
    private String[] tab_array;
    private TabLayout mToolbarTab;
    /**
     * 图标
     */
    private int[] tabIcons = {
            R.drawable.tab_home,
            R.drawable.tab_weichat,
            R.drawable.tab_user
    };

    @Override
    protected boolean exit() {
        return false;
    }

    @Override
    public void onMultiClick(View v) {

    }

    @Override
    public int initCView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVM() {
        return BR.viewmodel;
    }

    @Override
    public void initData(View v) {
        NetStatusBus.getInstance().register(this);

        tab_array = getResources().getStringArray(R.array.tab_main);
        fragments.clear();
        fragments.add(FragmentUtils.getHomeFragment());
        fragments.add(FragmentUtils.getChatFragment());
        fragments.add(FragmentUtils.getMeFragment());
        mToolbarTab = findViewById(R.id.toolbar_tab);
        mMViewPager = findViewById(R.id.mViewPager);
        setViewPagerAdapter();
        setTabBindViewPager();
        setItem();


    }

    @Override
    public MainModel initViewModel() {
        return super.initViewModel();
    }

    /**
     * 设置adapter
     */
    private void setViewPagerAdapter() {
        mDemandAdapter = new DemandAdapter(getSupportFragmentManager());
        mMViewPager.setAdapter(mDemandAdapter);
    }

    private void setTabBindViewPager() {
        mToolbarTab.setupWithViewPager(mMViewPager);
    }

    private void setItem() {
        /**
         * 一定要在设置适配器之后设置Icon
         */
        for (int i = 0; i < mToolbarTab.getTabCount(); i++) {
            mToolbarTab.getTabAt(i).setCustomView(getTabView(i));
        }
    }

    /**
     * 适配器
     */
    public class DemandAdapter extends FragmentStatePagerAdapter {
        public DemandAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        ImageView tab_image = view.findViewById(R.id.tab_image);
        TextView tab_text = view.findViewById(R.id.tab_text);
        tab_image.setImageResource(tabIcons[position]);
        tab_text.setText(tab_array[position]);
        return view;
    }

    @Override
    protected void onStop() {
        super.onStop();
        NetStatusBus.getInstance().unregister(this);
    }
}